package xiaohao.order.manager.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import xiaohao.order.constants.RateStatus;
import xiaohao.order.constants.TradeStatus;
import xiaohao.order.dao.TradeDao;
import xiaohao.order.domain.TradeQuery;
import xiaohao.order.domain.result.TradeQueryResult;
import xiaohao.order.exception.TradeException;
import xiaohao.order.manager.TradeQueryManager;

import com.taobao.api.domain.Order;
import com.taobao.api.domain.Trade;

public class DefaultTradeQueryManager implements TradeQueryManager {
	
	private final Logger logger = Logger.getLogger(getClass());

	@Override
	public TradeQueryResult getAllTrades(String sessionKey) throws TradeException {
		return tradeDao.getTradesBy(new TradeQuery(), sessionKey);
	}
	
	@Override
	public TradeQueryResult getTradesBy(TradeQuery query, String sessionKey)
			throws TradeException {
		if (StringUtils.isNotEmpty(query.getAuctionTitle())) {
			query.setPageSize(100L);
		}
		
		TradeQueryResult result = tradeDao.getTradesBy(query, sessionKey);
		List<Trade> trades = result.getTrades();
		
		final String auctionTitle = query.getAuctionTitle();
		final String rateStatus = query.getRateStatus();
		
		if (trades != null) {
			long before = trades.size();
			
			CollectionUtils.filter(trades, new Predicate() {
				
				@Override
				public boolean evaluate(Object object) {
					Trade input = (Trade) object;
					if (RateStatus.RATE_ALL.name().equals(rateStatus)) {
						if (!input.getBuyerRate() || !input.getSellerRate()) {
							return false;
						}
					}
					if (StringUtils.isNotEmpty(auctionTitle)) {
						boolean hasTitle = false;
						for (Order order : input.getOrders()) {
							if (order.getTitle().contains(auctionTitle)) {
								hasTitle = true;
								break;
							}
						}
						return hasTitle;
					}
					return true;
				}
			});
			
			long after = trades.size();
			logger.info(before + " --- " + after);
			if (StringUtils.isNotEmpty(auctionTitle)) {
				result.setTotalResults(after);
			}
			
			logger.info(result.getTotalResults());
		}
		
		return result;
	}
	
	@Override
	public Trade getTradeByTid(long tid, String sessionKey)
			throws TradeException {
		return tradeDao.getTradeByTid(tid, sessionKey);
	}

	@Override
	public TradeQueryResult getBuyerCanRateTrades(String sessionKey) throws TradeException {
		TradeQuery query = new TradeQuery();
		query.setRateStatus(RateStatus.RATE_UNBUYER.name());
		query.setStatus(TradeStatus.TRADE_FINISHED.name());
		return tradeDao.getTradesBy(query, sessionKey);
	}
	
	@Override
	public TradeQueryResult getTradesForSpending(TradeQuery query,
			String sessionKey) throws TradeException {
		return tradeDao.getTradesForSpending(query, sessionKey);
	}
	
	@Override
	public TradeQueryResult getTradesForTradeCycle(TradeQuery query,
			String sessionKey) throws TradeException {
		return tradeDao.getTradesForTradeCycle(query, sessionKey);
	}
	
	@Override
	public TradeQueryResult getTradesForTradeAmount(TradeQuery query, String sessionKey)
			throws TradeException {
		return tradeDao.getTradesForTradeAmount(query, sessionKey);
	}
	
	@Override
	public Trade getTradeByTidForPromotion(long tid, String sessionKey) throws TradeException {
		return tradeDao.getTradeByTidForPromotion(tid, sessionKey);
	}
	
	private TradeDao tradeDao;

	public void setTradeDao(TradeDao tradeDao) {
		this.tradeDao = tradeDao;
	}
	
}
